function keys(obj) {

    var keys = [];

    for(var key in obj) {
        if(obj.hasOwnProperty(key)) {
            keys.push(key);
        }
    }

    return keys;
}

function ToggleList(event,id) {

  if( event != null ) {
     event.preventDefault();
  }

  var divid = document.getElementById("ARCHIVE-DIVID-" + id);
  var ulid = document.getElementById("ARCHIVE-ULID-" + id);

  if( divid.style.display == "block" ) {
    divid.style.display = "none";
    ulid.style.listStyleType = 'disc';
  } else {
    divid.style.display = "block";
    ulid.style.listStyleType = "circle";
  }

};

function DisplayArchive(TotalFeed) {

  var Posts = {};

  if("entry" in TotalFeed.feed) {

    var TotalPosts=TotalFeed.feed.entry.length;

    for(var count=0; count < TotalPosts ; count++) {

      var post = TotalFeed.feed.entry[count];

      var pid = "pid" + count;
      var ptitle = post.title.$t;
      var pyear = post.published.$t.substring(0,4); 
      var pmonth = post.published.$t.substring(5,7);
      var pday = post.published.$t.substring(8,10);

      var plink;
      for(var LinkNum=0; LinkNum < post.link.length; LinkNum++) {
        if(post.link[LinkNum].rel == "alternate") {
          plink = post.link[LinkNum].href;
          break;
        }
      }

      if( Posts[pyear] == undefined ) { Posts[pyear] = {}; }
      if( Posts[pyear][pmonth] == undefined ) { Posts[pyear][pmonth] = {}; }
      if( Posts[pyear][pmonth][pday] == undefined ) { Posts[pyear][pmonth][pday] = {};}
      if( Posts[pyear][pmonth][pday][pid] == undefined ) { Posts[pyear][pmonth][pday][pid] = {};}

      Posts[pyear][pmonth][pday][pid]["title"] = ptitle;
      Posts[pyear][pmonth][pday][pid]["link"] = plink;

    }

  }

  var MonthNames=["January","February","March","April","May","June","July","August","September","October","November","December"];

  var expandYear = 0;
  var expandMonth = 0;

  var Years = keys(Posts).sort(function(a,b){return b-a});
  for(var i = 0; i < Years.length; ++i) {

    pyear = Years[i];

    document.write('<ul id="ARCHIVE-ULID-' + pyear + '"><li><a href="#" onclick=ToggleList(event,"' + pyear + '")>' + pyear + '</a></li>');
    document.write('<div class="archive-list" id="ARCHIVE-DIVID-' + pyear + '">');

    if ( expandYear == 0 ) { expandYear = pyear ; }

    var Months = keys(Posts[pyear]).sort(function(a,b){return b-a});

    for(var j = 0; j < Months.length; j++ ) {

      pmonth = Months[j];

      if ( expandMonth == 0 ) { expandMonth = pmonth ; }

      document.write('<ul style="list-style-type:disc;" id="ARCHIVE-ULID-' + pyear + '-' + pmonth +'">');
      document.write('<li><a href="#" onclick=ToggleList(event,"' + pyear + "-" + pmonth+ '")>' + MonthNames[parseInt(pmonth,10)-1] + "</a></li>" );
      document.write('<div class="archive-list" id="ARCHIVE-DIVID-' + pyear + "-" + pmonth + '">');

      for(var pday in Posts[pyear][pmonth]) {
        for(var pid in Posts[pyear][pmonth][pday]) {
          document.write('<ul style="list-style-type:circle;"><li><a target="_blank" href="' + Posts[pyear][pmonth][pday][pid]["link"] + '">' + Posts[pyear][pmonth][pday][pid]["title"] + '</a></li>' );
          document.write("</ul>");    
        }
      }

      document.write("</div></ul>");    

    }

    document.write("</div></ul>");    

  }

  // Initialize the list
  ToggleList(null,expandYear);
  ToggleList(null,expandYear + '-' + expandMonth);

};
